package api

import (
	"Suscan/models"
	"Suscan/pkg/e"
	"Suscan/pkg/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func GetUrl(c *gin.Context) {
	url := c.Query("url")
	ip := c.Query("ip")
	create_time := c.Query("create_time")
	update_time := c.Query("update_time")

	maps := make(map[string]interface{})


	if url != "" {
		maps["url"] = url
	}

	if ip != "" {
		maps["ip"] = ip
	}

	if create_time != "" {
		maps["create_time"] = create_time
	}

	if update_time != "" {
		maps["update_time"] = update_time
	}

	code := e.SUCCESS

	pageSize, _  := strconv.Atoi(c.Query("pageSize"))

	data := models.GetUrl(utils.GetPage(c), pageSize, maps)
	total := models.GetUrlTotal(maps)

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
		"total":total,
	})
}


type Url struct {
	ID []int `json:"id"`
}

func DeleteUrl(c *gin.Context) {
	json := Url{}
	c.BindJSON(&json)
	var code int
	data :=make(map[string]string)
	for _,v := range json.ID {
		a ,id := models.DeleteUrl(v)
		if a {
			code = e.SUCCESS
		}else {
			code = e.ERROR
			data["id"] = strconv.Itoa(id)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}